﻿function $(value){
 return document.getElementById(value);
}
//得到售票ID
function ticketOrderInfo(templanName) {
	this._WicketId; 
	this._result;
	this._value
	//生成下拉列表
	this.creatSelectData = function(templanName) {
	    var oOption, oSelect, result;
	    if (document.getElementById(templanName).childNodes.length > 0) {
	        document.getElementById(templanName).innerHTML = "";
	    }
	    oSelect = document.all(templanName);
	    result = this._result;
	    if (this._result.length != 0) {
	        for (var i = 0; i < this._result.length; i++) {
	            oOption = document.createElement("OPTION");
	            if (templanName == "wicket" || templanName == "wicketTow") {
	                oOption.id = this._result[i].AgentID;
	                oOption.value = i;
	                oOption.text = this._result[i].AgentName;
	            }
	            else {
	                oOption.text = this._result[i];
	                oOption.value = this._result[i];
	            }
	            oSelect.add(oOption);
	        }
	    }
	    else {
	        oOption = document.createElement("OPTION");
	        oOption.value = 0;
	        oOption.text = "不限";
	        oSelect.add(oOption);
	    }

	}
	//加载TAB1内容,售票点各种信息　
	this.loadWicketInfo = function(templanName) {

	    if (this._result == "") {
	        if (templanName == "address") {
	            document.getElementById(templanName).innerText = "";
	        }
	        else {
	            document.getElementById(templanName).firstChild.childNodes[2].childNodes[0].childNodes[1].innerText = "";
	            document.getElementById(templanName).firstChild.childNodes[2].childNodes[1].childNodes[1].innerText = "";
	            document.getElementById(templanName).firstChild.childNodes[3].firstChild.childNodes[1].innerText = "";
	        }
	    } else {
	        if (templanName == "address") {
	            document.getElementById(templanName).LoginName = this._result[this._value].LoginName;
	            document.getElementById(templanName).AgentName = this._result[this._value].AgentName;
	            document.getElementById(templanName).innerText = this._result[this._value].Address;

	        } else {
	            document.getElementById(templanName).firstChild.childNodes[1].childNodes[1].LoginName = this._result[this._value].LoginName;
	            document.getElementById(templanName).firstChild.childNodes[1].childNodes[1].AgentName = this._result[this._value].AgentName;
	            document.getElementById(templanName).firstChild.childNodes[2].childNodes[0].childNodes[1].innerText = this._result[this._value].Linkman;
	            document.getElementById(templanName).firstChild.childNodes[2].childNodes[1].childNodes[1].innerText = this._result[this._value].WorkHours;
	            document.getElementById(templanName).firstChild.childNodes[3].firstChild.childNodes[1].innerText = this._result[this._value].Address;
	            document.getElementById("imgMap").title = this._result[this._value].Nearby;
	        }
	    }
	}
    //加载TAB2内容.查询火车情况
	this.loadTrainList = function(templanName) {
	    var oTable = document.getElementById(templanName).tBodies[0];
	    while (oTable.rows.length > 1) {
	        oTable.deleteRow();
	    }
	    var oFrament = document.createDocumentFragment();
	    var oTr = document.getElementById("ListTemplan").cloneNode(true);
	    oTr.style.display = "block";
	    for (var i = 0; i < this._result.length; i++) {
	        var oRow = oTr.cloneNode(true);
	        oRow.childNodes[0].id = this._result[i].LineID;
	        oRow.childNodes[0].firstChild.onclick = function() { writeInfo() };
	        oRow.childNodes[0].childNodes[1].innerText = this._result[i].TrainNumber;
	        oRow.childNodes[1].innerText = this._result[i].StartStation;
	        oRow.childNodes[2].innerText = this._result[i].EndStation;
	        oRow.childNodes[3].innerHTML = "<a  style='height:30px'  target='_blank'  href='TrainNumberSearch.aspx?TrainNumber=" + this._result[i].TrainNumber + "'>" + this._result[i].TrainNumber + "</a>";
	        oRow.childNodes[4].innerText = this._result[i].StartTime;
	        oRow.childNodes[5].innerText = this._result[i].EndTime;
	        oRow.childNodes[6].innerText = this._result[i].TrainType;
	        if (this._result[i].M_yz > 0) {
	            oRow.childNodes[7].innerText = "硬座：" + this._result[i].M_yz;
	        }
	        if (this._result[i].M_rz > 0) {
	            oRow.childNodes[7].innerText += "软座：" + this._result[i].M_rz;
	        }
	        if (this._result[i].M_yw > 0) {
	            oRow.childNodes[7].innerText += "硬卧：" + this._result[i].M_yw;
	        }
	        if (this._result[i].M_rw > 0) {
	            oRow.childNodes[7].innerText += "软卧：" + this._result[i].M_rw;
	        }

	        oFrament.appendChild(oRow);
	    }
	    document.getElementById(templanName).tBodies[0].appendChild(oFrament);
	} 
        
}
var WicketInfo = new ticketOrderInfo();
var getTotal = 0;  //没有被客户改的金额
var changeTotal = 0;
var wicketResult = "";
//得到出行城市
function GetCity() {
    AjaxWebService.GetAllCity(onGetAllCity);    
}
function onGetAllCity(result) {
    WicketInfo._result = result;
    WicketInfo.creatSelectData("startCity");
   
}
//得到售票点
function GetWicket(value) { 
        AjaxWebService.GetAgentListByCity(value, onGetWicket);
   
}
function onGetWicket(result) { 
    WicketInfo._result = result;
    WicketInfo.creatSelectData("wicket"); 
    var value = 0;
    for (var i = 0; i < document.getElementById("wicket").childNodes.length; i++) {
        if (document.getElementById("wicket").childNodes[i].selected == true) {
            value = document.getElementById("wicket").childNodes[i].value
            returnWicket(document.getElementById("wicket").childNodes[i].innerText);
            changeWicketInfo(result, document.getElementById("wicket").childNodes[i].value,"Info"); 
        }
    }
    wicketResult = result;
}
//选择不同的售票点
function changeWicket(value, templan, tagName) { 
 
    for (var i = 0; i < document.getElementById(templan).childNodes.length; i++) {
        if (document.getElementById(templan).childNodes[i].selected == true) { 
            value = document.getElementById(templan).childNodes[i].value
            changeWicketInfo(wicketResult, document.getElementById(templan).childNodes[i].value, tagName);
        }
    }
}
//更新售票点信息
function changeWicketInfo(result, value, tagName) {  
    WicketInfo._result = result;
    WicketInfo._value = value;
    WicketInfo.loadWicketInfo(tagName);
    if (tagName == "address") { 
        if (document.getElementById('address').LoginName != undefined) {
            JsToFlashCode(document.getElementById('address').LoginName, document.getElementById('address').AgentName);
        }
    }
}
var wicketAddress = "";
//返回选择的售票点
function returnWicket(value) { 
    wicketAddress =  value;
}

//查询列车信息加载列表
//按地点查询
function GetTrainList() {
    var start = document.getElementById("start").value;
    var end = document.getElementById("end").value;
    if (start != "" && end != "") {
        AjaxWebService.SearchByTrianStation(start, end, onGetTrainList);
    }
}
function onGetTrainList(result) {
 
    WicketInfo._result = result
    WicketInfo.loadTrainList("trainList");
}
//按车次查询
function GetTrainNumber() {
    var Number = document.getElementById("TrainNumber").value;    
    if (Number != "") {
        AjaxWebService.SearchByTrianNumber(Number, onGetTrainNumber);
    }
}  
function onGetTrainNumber(result) {

    WicketInfo._result = result
    WicketInfo.loadTrainList("trainList");
}
//加载TAB3的下拉列表
function CityChoose() {
    AjaxWebService.GetAllCity(onCityChioose); 
}
function onCityChioose(result) {
    WicketInfo._result = result;
    WicketInfo.creatSelectData("startCityTow");     
    if (document.getElementById("startCity").options[document.getElementById("startCity").options.selectedIndex].selected == true) {
        document.getElementById("startCityTow").options[document.getElementById("startCity").options.selectedIndex].selected = true;
    } 
    wicketChoose(document.getElementById("startCityTow").options[document.getElementById("startCity").options.selectedIndex].text) 
}
//加载TAB3的第二个下拉列表与地址信息
function wicketChoose(value) {
    AjaxWebService.GetAgentListByCity(value, onWicketChoose);
    
}
function onWicketChoose(result) { 
    WicketInfo._result = result;
    WicketInfo.creatSelectData("wicketTow");
    if (document.getElementById("wicket").options[document.getElementById("wicket").options.selectedIndex].selected == true) {
        document.getElementById("wicketTow").options[document.getElementById("wicket").options.selectedIndex].selected = true;
    }
    var value = 0; 
    for (var i = 0; i < document.getElementById("wicketTow").childNodes.length; i++) {
        if (document.getElementById("wicketTow").childNodes[i].selected == true) {
            WicketInfo._value = document.getElementById("wicketTow").childNodes[i].value
            changeWicketInfo(result, document.getElementById("wicketTow").childNodes[i].value, "address"); 
        }
    }
    wicketResult = result;     
}
//TAB4信息
function orderInfo() {

    var seatValue = ""; 
    if (wicketAddress != "") {
        document.getElementById("spanAgentName").innerText = wicketAddress + ";";
    }
    else {
        document.getElementById("spanAgentName").innerText = "";
    }
    if (document.getElementById("checkChildren").checked == true) {
        document.getElementById("spanChild").innerText = "儿 童 票";
    } else {
    document.getElementById("spanChild").innerText = "";
    }
    
    for (var i = 0; i < $("seatCategor").childNodes[1].childNodes.length; i++) { 
        if ($("seatCategor").childNodes[1].childNodes[i].selected == true) {
            seatValue = $("seatCategor").childNodes[1].childNodes[i].value;             
        }
    }
    if (document.getElementById("txtTicketDate").value != "" && document.getElementById("txtTicketDate").value != "yyyy-mm-dd") { 
        document.getElementById("commitInfo").childNodes[0].innerText = document.getElementById("txtTicketDate").value;
    }
    if (document.getElementById("shuLiang").value != "") {
        document.getElementById("commitInfo").childNodes[5].firstChild.value = document.getElementById("shuLiang").value;
    }
    for (var i = 1; i < document.getElementById("trainList").firstChild.childNodes.length; i++) {
        if (document.getElementById("trainList").firstChild.childNodes[i].firstChild.firstChild.checked == true) {  
            document.getElementById("commitInfo").childNodes[1].innerText = document.getElementById("trainList").firstChild.childNodes[i].firstChild.childNodes[1].innerText;
            document.getElementById("commitInfo").childNodes[2].innerText = document.getElementById("trainList").firstChild.childNodes[i].childNodes[1].innerText;
            document.getElementById("commitInfo").childNodes[3].innerText = document.getElementById("trainList").firstChild.childNodes[i].childNodes[2].innerText;
            document.getElementById("commitInfo").childNodes[4].innerText = seatValue;
        }
    }

}
//车次,车站写入
function writeInfo() { 
    for (var i = 1; i < document.getElementById("trainList").firstChild.childNodes.length; i++) { 
        if (document.getElementById("trainList").firstChild.childNodes[i].firstChild.firstChild.checked == true) {
            if (document.getElementById("TrainNumber").value == "") { 
            document.getElementById("TrainNumber").value = document.getElementById("trainList").firstChild.childNodes[i].firstChild.innerText;
            }
            else{
                document.getElementById("start").value = document.getElementById("trainList").firstChild.childNodes[i].childNodes[1].innerText;
                document.getElementById("end").value = document.getElementById("trainList").firstChild.childNodes[i].childNodes[2].innerText;
            }
        }
    }
}
//计算价格
function FormulaTotal() {
    var QTY = 1;
    var Price = 0;
    var other = 0;
    if (document.getElementById("commitInfo").childNodes[1].innerText != "") {
        if (isNaN(document.getElementById("QTY").value) == false && isNaN(document.getElementById("Price").value) == false) {
            QTY = document.getElementById("QTY").value;
            Price =document.getElementById("Price").value;
        }
        if (isNaN(document.getElementById("txtOther").value) == false) {
            other = document.getElementById("txtOther").value;
        }
    }
    else {
        alert("对不起,数据不完整");
        return;
    }
    
   
    if (QTY.indexOf(".") != -1) {
        alert("数量不可以有小数.请检查后重新填写");
        document.getElementById("QTY").select();
        document.getElementById("QTY").focus();
        return;
    }
    if (Price.indexOf(".") != -1) {
        if (Price.substring(Price.indexOf(".") + 1, Price.length).length > 2) {
            alert("对不起,你的票价小数位太长了.请检查后重新填写");
            document.getElementById("Price").select();
            document.getElementById("Price").focus();
            return;
        }
    }
    if (other.indexOf(".") != -1) {
        if (other.substring(other.indexOf(".") + 1, other.length).length > 2) {
            alert("对不起,你的其它费小数位太长了.请检查后重新填写");
            document.getElementById("txtOther").select();
            document.getElementById("txtOther").focus();
            return;
        }
    }
    if (QTY != "" && Price != "") {
        var serverTotal = 0;
        if (other > 0) {
            serverTotal = ((QTY * Price + 5 + parseFloat(other)) * 0.01);
            document.getElementById("total").value = (serverTotal + (QTY * Price + 5 + parseFloat(other))).toFixed(2)
        }
        else {
            serverTotal = ((QTY * Price + 5) * 0.01);
            document.getElementById("total").value = ((QTY * Price + 5 + serverTotal)).toFixed(2)
        }
        document.getElementById("commitInfo").childNodes[9].innerText = serverTotal.toFixed(2); 
    }
    getTotal = document.getElementById("total").value;
}
 
//提交订单
function commitOrder() {
    var UserName = "";
    var UserTel = "";
    var Remark = "";
    var WicketQTY = "";
    var childrenTicket = true;
    var agentid = "不限";
    for (var i = 0; i < document.getElementById("wicket").childNodes.length; i++) {
        if (document.getElementById("wicket").childNodes[i].selected == true) {
            agentid = document.getElementById("wicket").childNodes[i].id;
        }
    }
    
    if (agentid == "不限" || agentid == "") {
        alert("对不起,请您选择你的出行城市和售票点");
        return;
    }
    if (document.getElementById("commitInfo").childNodes[1].innerText == "" || document.getElementById("commitInfo").childNodes[9].innerText == "") {
        alert("对不起,你要提交的订单信息不完整");
        return;
    }
    
    if (document.getElementById("Name").value != "" && document.getElementById("Tel").value != "") {
        UserName = document.getElementById("Name").value;
        UserTel = document.getElementById("Tel").value;
    }
    else {
        alert("请填写完全.加*为必填项");
        return;
    } 
    Remark = document.getElementById("remark").value;

    if (document.getElementById("spanChild").innerText == "") {
        childrenTicket = false
    }
   
    var Order = {
        UserID: "0",
        AgentID: agentid,
        TicketDate: document.getElementById("commitInfo").childNodes[0].innerText,
        StartStation: document.getElementById("commitInfo").childNodes[2].innerText,
        EndStation: document.getElementById("commitInfo").childNodes[3].innerText,
        TrainNumber: document.getElementById("commitInfo").childNodes[1].innerText,
        UserName: UserName,
        UserTelphone: UserTel,
        Remark: Remark,
        Pew: document.getElementById("commitInfo").childNodes[4].innerText,
        UnitPrice: document.getElementById("Price").value,
        TotalMoney:document.getElementById("txtOther").value,
        PayMoney: document.getElementById("total").value,
        PayPoundage: document.getElementById("commitInfo").childNodes[9].innerText,
        Amount: document.getElementById("QTY").value,
        isCHD: childrenTicket
    }; 
    AjaxWebService.InsertOrder(Order,onCommitOrder)
}
function onCommitOrder(result) { 
    if (result <= 0) {
        alert("订单提交失败,請稍后再试");
        return;
    }
    else {
        document.getElementById("btnCommit").disabled = true
          window.location = "WaitPay.aspx?OrderID=" + escape(result);    
    }
}
 